﻿import http from '@/utils/http.js'
import Vue from 'vue'
import { IsNull } from '@/utils/utils'
import LoginService from './login.service'
import Global from '@/utils/Global.js'

export default {
  entBaseInfo: null,
  entOrganizationInfo: null,
  sysSetting: null,
  // 私钥
  privateKey: null,

  // / 获取logo
  // async getLogoImage() {
  //     await http.loadAppJson();
  //     return Global.addressip + '/api/System/GetEntLogoImg/EntLogo';
  // },
  // /// 获取登录背景图
  // async getLgoinBackgroundImage() {
  //     await http.loadAppJson();
  //     return Global.addressip + '/api/System/GetEntLogoImg/LoginLogo';
  // },

  // 获取私钥
  async getPrivateKey() {
    if (this.privateKey) {
      return Promise.resolve(this.privateKey)
    } else {
      // const res = await http.GET(`api/System/GetPrivateKey`);
      // if (res.Success) {
      //     this.privateKey = res.Result;
      //     return this.privateKey;
      // } else {
      //     return Promise.reject('私钥获取失败');
      // }

      const res = await this.getEntBaseInfo()
      if (res && res.PrivateKey) {
        this.privateKey = this.entBaseInfo.PrivateKey
        return Promise.resolve(this.privateKey)
      } else {
        return Promise.reject('私钥获取失败')
      }
    }
  },

  // 获取企业信息
  async getEntBaseInfo() {
    if (!this.entBaseInfo) {
      const res = await http.GET(Global.addressip + '/api/System/GetEntBaseInfo')
      if (res.Success) {
        this.entBaseInfo = res.Result
        this.privateKey = this.entBaseInfo.PrivateKey
        return this.entBaseInfo
      }
    } else {
      return Promise.resolve(this.entBaseInfo)
    }
  },
  // 获取企业信息
  getMessageSet() {
    return http.GET(Global.addressip + '/api/System/GetMessageSet')
  },
  // 保存消息设置
  saveMessageSet(parameters) {
    return http.POST(Global.addressip + '/api/System/SaveMessageSet', parameters)
  },
  // 获取企业信息
  async getEntOrganizationInfo() {
    if (!this.entOrganizationInfo) {
      const res = await http.GET(Global.addressip + '/api/System/GetEntOrganizationInfo')
      if (res.Success) {
        this.entOrganizationInfo = res.Result
        return this.entOrganizationInfo
      }
    } else {
      return Promise.resolve(this.entOrganizationInfo)
    }
  },
  // 获取企业配置中心数据
  GetSettingCenterInfo() {
    return http.GET(Global.addressip + '/api/System/GetSettingCenter')
  },
  GetLicenseSignSetting() {
    return http.GET(Global.addressip + `/api/System/GetLicenseSignSetting`)
  },
  SaveLicenseSignSetting(license) {
    return http.POST(Global.addressip + `/api/System/SaveLicenseSignSetting`, license)
  },
  getSettingCenter() {
    return http.GET(Global.addressip + `/api/System/GetSettingCenter`)
  },
  // 保存系统参数和组织同步
  saveCustomSetting(param) {
    return http.POST(Global.addressip + `/api/System/SaveCustomSetting`, param)
  },
  getCustomSettingValue() {
    return http.GET(Global.addressip + '/api/System/GetCustomSettingValue')
  },
  async getSystemSetting() {
    if (!this.sysSetting) {
      const res = await http.GET(Global.addressip + '/api/System/GetSystemSetting')
      if (res.Code = 200) {
        debugger
        this.sysSetting = res.Result
        return this.sysSetting
      }
    } else {
      return Promise.resolve(this.sysSetting)
    }
  },
  getThirdPartyAuth(objectId) {
    return http.GET(Global.addressip + `/api/System/GetThirdPartyAuth/${objectId}`)
  },
  deleteThirdPartyAuth(objectId) {
    return http.GET(Global.addressip + `/api/System/DeleteThirdPartyAuth/${objectId}`)
  },
  getAllThirdPartyAuths() {
    return http.GET(Global.addressip + `/api/System/GetAllThirdPartyAuths`)
  },
  thirdSystemNameExist(systemName, objectId) {
    let api = Global.addressip + `/api/System/ThirdSystemNameExist/${systemName}`
    if (!IsNull(objectId)) {
      api += `/${objectId}`
    }
    return http.GET(api)
  },
  thirdSystemCodeExist(systemCode, objectId) {
    let api = Global.addressip + `/api/System/ThirdSystemCodeExist/${systemCode}`
    if (!IsNull(objectId)) {
      api += `/${objectId}`
    }
    return http.GET(api)
  },
  addThirdPartyAuth(thirdPartyAuth) {
    return http.POST(Global.addressip + '/api/System/AddThirdPartyAuth', thirdPartyAuth)
  },
  updateThirdPartyAuth(thirdPartyAuth) {
    return http.POST(Global.addressip + '/api/System/UpdateThirdPartyAuth', thirdPartyAuth)
  },
  createRSAKey() {
    return http.GET(Global.addressip + `/api/System/CreateRSAKey`)
  },
  getGetWatermarkSet() {
    const userId = LoginService.userInfo.ObjectID
    return http.GET(Global.addressip + `/api/System/GetWatermarkSet/${userId}`)
  }
}
